/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.zjdt.epserver.db.dao4mysql;

import com.zjdt.epserver.db.dao.FactoryDao;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 *
 * @author dusin
 */
public class FactoryDaoImpl implements FactoryDao{

    static Logger logger = Logger.getLogger(FactoryDaoImpl.class);
    private JdbcTemplate jdbcTemplate;

    public FactoryDaoImpl(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }   
        
    @Override
    public boolean doCreate(Object[] bean) {
        boolean flag = true;
        String sql = null;
        sql = "insert into factory(code,name,address,longitude,latitude,typeCode,plateStart) values(?,?,?,?,?,?,?)";
        int rows = jdbcTemplate.update(sql, bean);
        if (rows <= 0) {
            flag = false;
        }
        return flag;
    }

    @Override
    public boolean doDeleteById(int id) {
        boolean flag = true;
        String sql = null;
        sql = "delete from factory where id=?";
        int rows = jdbcTemplate.update(sql, id);
        if (rows <= 0) {
            flag = false;
        }
        return flag;
    }

    @Override
    public boolean doUpdateById(Object[] bean) {
        boolean flag = true;
        String sql = null;
        sql = "update factory set code=?,name=?,address=?,typeCode=?,plateStart=? where id=?";
        int rows = jdbcTemplate.update(sql, bean);
        if (rows <= 0) {
            flag = false;
        }
        return flag;
    }

   

    @Override
    public Map<String, Object> findById(int id) {
        String sql = null;
        try{
            sql = "select * from  vfactory where id = ?";
            return jdbcTemplate.queryForMap(sql,id);        
        }catch(EmptyResultDataAccessException e){
            return null;
        }
        
    }

    @Override
    public List<Map<String, Object>> findAll() {        
        String sql = "select * from vfactory";
        return jdbcTemplate.queryForList(sql);
    }

   
    @Override
    public List<Map<String, Object>> findAll4Combo() {
        String sql = "select code,name,typeCode from factory";
        return jdbcTemplate.queryForList(sql);
    }
}
